//queue.h -- queue template with nested calss
#ifndef QUEUE_H_H
#define QUEUE_H_H
template <class Item>
class QueueTP
{
private:
      enum { Q_SIZE = 10};
      
      class Node
      {
      public:
         Item item;
         Node *next;
         Node(const Item & i):item(i),next(0){}
      };
      Node * front;
      Node * rear;
      int items; //当前item的个数
      const int qsize; //队列最大个数
      QueueTP(const QueueTP & q): qsize(0){}
      QueueTP & operator=(const QueueTP & q) {return *this;}
public:
    QueueTP(int qs = Q_SIZE);
    ~QueueTP();
    bool isempty() const
    {
        return items == 0;
    }
    bool isfull() const
    {
        return items == qsize;
    }
    int queuecount() const
    {
        return items;
    }
    bool enqueue(const Item & item);
    bool dequeue(Item & item);
};

template <class Item>
QueueTP<Item>::QueueTP(int qs):qsize(qs)
{
    front = 0;
    rear = 0;
    items = 0;
}
template <class Item>
QueueTP<Item>::~QueueTP()
{
  Node *temp;
  while (front != 0)
  {
      temp = front;
      front = front->next;
      delete temp;
  }

}

template <class Item>
bool QueueTP<Item>::enqueue(const Item &item)
{
    if (isfull())
    {
        return false;
    }
    Node *newNode = new Node(item);
    items ++;
    if (front == 0)
    {
        front = newNode;
    }else{
        //先让新node入队列
        rear->next = newNode;
    }
    //将新node作为rear
    rear = newNode;
    return true;
}

template <class Item>
bool QueueTP<Item>::dequeue(Item &item)
{
    if (items == 0)
    {
        return false;
    }
    //要返回出队列的item
    item = front->item;
    Node * aNode = front;
    front = front->next;
    delete aNode;
    items --;
    return true;
}

#endif

